package com.atguigu.msmservice.controller;

import cn.hutool.core.util.RandomUtil;
import com.atguigu.commonutils.Result;
import com.atguigu.msmservice.service.MsmService;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/msmservice/sendMessage")
@CrossOrigin //跨域
@Slf4j
public class SendMessageController {

	@Resource
	private MsmService msmService;

	@Resource
	private RedisTemplate<String, String> redisTemplate;

	@GetMapping(value = "/send/{phone}")
	public Result code(@PathVariable String phone) {
		String code = redisTemplate.opsForValue().get(phone);
		if (!StringUtils.isEmpty(code)) {
			return Result.ok();
		}
		/**
		 * 生成四位验证码
		 */
		code = RandomUtil.randomNumbers(6);
		boolean isSend = msmService.send(phone, " SMS_269500305", code);

		return Result.ok().message("发送成功");

	}

}
